package two.chapter_4.c4_1;

public class Cycle {

    private boolean[] marked;
    private boolean hasCycle;

    public Cycle(Graph g) {
        marked = new boolean[g.v()];
        for (int s = 0; s < g.v(); s++) {
            if (!marked[s])
                dfs(g, s);
        }

    }

    private void dfs(Graph g, int v) {
        marked[v] = true;
        for (int w : g.adj(v)) {
            if (!marked[w]) {
                dfs(g, w);
            } else if (v != w) {
                hasCycle = true;
            }
        }
    }

    public boolean hasCycle() {
        return hasCycle;
    }
}
